package Browser;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import crawler.src.Filter;

public class Password extends JFrame{
	
	final static long serialVersionUID = 12345678L; 
	
	private Filter filter;
	
	private JPanel mainPanel = new JPanel(new GridLayout(4, 0));
	private JPanel createPanel = new JPanel(new GridLayout(2,2));
	private JPanel validatePanel = new JPanel(new GridLayout(1, 1));
	
	private JLabel passTitle = new JLabel("", SwingConstants.CENTER);
	private JLabel confirmPass = new JLabel("Confirm Password:");
	private JLabel insertPass = new JLabel("Insert Password:");
	private JLabel infoLabel = new JLabel("", SwingConstants.CENTER);
	private JPasswordField passField = new JPasswordField();
	private JPasswordField passConfirmation = new JPasswordField();
	private JButton confirm = new JButton("Confirm");
	
	public Password() {
		
		setTitle("Password");
		setDefaultLookAndFeelDecorated(true);
		setSize(300, 175);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		try {
			filter = new Filter();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (filter.hasPassword()) {
			passTitle.setText("Enter your Password");
			
			confirm.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) { 
					boolean verified = filter.verifyPass(passField.getPassword());
					
					if (verified) {
						Definitions defWindow = new Definitions();
						dispose();
					}
					else {
						infoLabel.setText("Incorrect Password");
					}
					
				}
			});
			
			passField.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) { 
					boolean verified = filter.verifyPass(passField.getPassword());
					
					if (verified) {
						Definitions defWindow = new Definitions();
						dispose();
					}
					else {
						infoLabel.setText("Incorrect Password");
					}
					
				}
			});
			
			passTitle.setBackground(Color.WHITE);
			validatePanel.setBackground(Color.WHITE);
			mainPanel.setBackground(Color.WHITE);
			
			mainPanel.add(passTitle);
			
			validatePanel.add(insertPass);
			validatePanel.add(passField);
			
			mainPanel.add(validatePanel);
			infoLabel.setForeground(Color.RED);
			mainPanel.add(infoLabel);
			mainPanel.add(confirm);
			
			add(mainPanel);
			setVisible(true);
		}
		else {
			passTitle.setText("Create a new Password");
			
			confirm.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (passField.getPassword().length  != 0) {
						if (passConfirmation.getPassword().length  != 0) {
							if (Arrays.equals(passField.getPassword(),passConfirmation.getPassword())) {
						
								filter.setPass(passField.getPassword());
								
								Definitions defWindow = new Definitions();
								dispose();
							} else {
								infoLabel.setText("Passwords have to be equals");
							}
						}
						else {
							infoLabel.setText("Your password must be verified");
						}
					}
					else {
						infoLabel.setText("Your need to choose a Password");
					}
					
				}
			});
			
			passField.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (passField.getPassword().length  != 0) {
						if (passConfirmation.getPassword().length  != 0) {
							if (Arrays.equals(passField.getPassword(),passConfirmation.getPassword())) {
						
								filter.setPass(passField.getPassword());
							
								Definitions defWindow = new Definitions();
								dispose();
							} else {
								infoLabel.setText("Passwords have to be iquals");
							}
						}
						else {
							infoLabel.setText("Your password must be verified");
						}
					}
					else {
						infoLabel.setText("Your need to choose a Password");
					}
				}
			});
			
			passConfirmation.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if (passField.getPassword().length  != 0) {
						if (passConfirmation.getPassword().length  != 0) {
							if (Arrays.equals(passField.getPassword(),passConfirmation.getPassword())) {
						
								filter.setPass(passField.getPassword());
						
								Definitions defWindow = new Definitions();
								dispose();
							} else {
								infoLabel.setText("Passwords have to be iquals");
							}
						}
						else {
							infoLabel.setText("Your password must be verified");
						}
					}
					else {
						infoLabel.setText("Your need to choose a Password");
					}
				}
			});
			
			passTitle.setBackground(Color.WHITE);
			createPanel.setBackground(Color.WHITE);
			mainPanel.setBackground(Color.WHITE);
			
			
			mainPanel.add(passTitle);
			
			createPanel.add(insertPass);
			createPanel.add(passField);
			createPanel.add(confirmPass);
			createPanel.add(passConfirmation);
			
			mainPanel.add(createPanel);
			infoLabel.setForeground(Color.RED);
			mainPanel.add(infoLabel);
			mainPanel.add(confirm);
			
			add(mainPanel);
			setVisible(true);
		}	
	}

}
